#ifdef GL_ES
precision mediump float;
#endif
uniform sampler2D s_texture;
uniform vec3 u_lightSource[24];
uniform vec4 u_lightColors[24];
uniform int u_lightSources;
varying vec3 N;
varying vec3 v;
varying vec2 v_texCoord;

void main() {
	vec4 color = texture2D(s_texture,v_texCoord);
	vec4 scene_ambient_light = vec4(0.2,0.1,0.2,1.0);
	vec4 material_diffuse = vec4(1.0);
	vec4 ambient = material_diffuse * scene_ambient_light;
	vec4 finalDiffuse = vec4(0.0);
	for (int i = 0; i < u_lightSources; i++) {
		vec3 dif = u_lightSource[i] - v;
		float len = length(dif);
		vec3 dir = dif/len;
		
		
		float light_radius = u_lightColors[i].a;
		vec4 lightColor = vec4(u_lightColors[i].x,u_lightColors[i].y,u_lightColors[i].z,1.0);
		float diffuseTerm = clamp(dot(N, dir), 0.0,1.0) * clamp((light_radius-len),0.0,1.0);
		finalDiffuse += lightColor * diffuseTerm * material_diffuse;
	}
	gl_FragColor = color*(ambient+finalDiffuse);
}
